package com.levi.producer;

import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.client.producer.SendStatus;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.common.message.MessageQueue;

import java.nio.charset.StandardCharsets;
import java.util.concurrent.TimeUnit;

/**
 * 发送单向消息  不关心消息是否发送成功，适合用于日志记录
 */

public class OnewayProducer {
    public static void main(String[] args) throws Exception {
        /**
         * 1.创建生产者，指定生产者组
         * 2.指定nameserver地址
         * 3.启动producer
         * 4.创建消息，指定topic，tag，消息体
         * 5.发送消息
         * 6.关闭producer
         */
        //1.创建生产者，指定生产者组
        DefaultMQProducer producer = new DefaultMQProducer("group1");
        //2.指定nameserver地址
        producer.setNamesrvAddr("192.168.177.129:9876");
        producer.setSendMsgTimeout(1000000);
        //3.启动producer
        producer.start();
        //4.创建消息，指定topic，tag，消息体
        for (int i = 1; i <= 20; i++) {
            Message message = new Message("base","tagA",("hello 单向发送的消息"+i).getBytes(StandardCharsets.UTF_8));
            //5.发送oneway消息，没有返回值
            producer.sendOneway(message);

            TimeUnit.SECONDS.sleep(1);
        }


        //6.关闭producer
        producer.shutdown();
    }

}
